package kuzha.runtime;

public class KzInt32 extends KzInteger {
	private static final int CACHE_MIN = -128;
	private static final int CACHE_MAX = 127;
	private static final KzInt32[] cache = new KzInt32[CACHE_MAX - CACHE_MIN + 1];
	
	static {
		for(int i = 0; i < cache.length; i++) {
			cache[i] = new KzInt32(i + CACHE_MIN);
		}
	}
	
	public static KzInt32 valueOf(int num) {
		if (CACHE_MIN <= num && num <= CACHE_MAX)
			return cache[num - CACHE_MIN];
		return new KzInt32(num);
	}
	
	public final int value;
	
	protected KzInt32(int value) {
		this.value = value;
	}
	
	@Override
	public boolean isTrue() {
		return value != 0;
	}
	
	@Override
	public Object toJavaObject() {
		return value;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + value;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final KzInt32 other = (KzInt32) obj;
		if (value != other.value)
			return false;
		return true;
	}
	
	@Override
	public Number getNumber() { return value; }
	@Override
	public String toString() { return Integer.toString(value); }
	@Override
	public String toHexString() { return Integer.toHexString(value); }
}
